package Servlet.admin;

import Utils.DruidUtils;
import domain.News;
import domain.SysUser;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/adminLogin")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../admin/login.jsp").forward(request, response);
//        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获取请求表单里面的数据
        String req_admin = request.getParameter("admin");
        String req_pwd = request.getParameter("pwd");
        String req_code = request.getParameter("code");
        String req_remember = request.getParameter("remember");

        boolean flag = false;

        // 获取session
        HttpSession session = request.getSession();
        // 创建Cookie对象，将sessionID存入cookie中
        Cookie cookie = new Cookie("JSESSIONID", session.getId());

        // 判断输入是否合法
        if (req_admin == "" && req_pwd == "" && req_code == "") {
            // 将错误信息储存到域对象中
            request.setAttribute("admin_login_from_info", "表单不能为空");
//            System.out.println("都为空");
            request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
            return;
        } else {
            request.setAttribute("admin_login_from_info", "");
        }

        // 将用户名和密码存入session
        session.setAttribute("admin_login_req", req_admin);
        session.setAttribute("admin_login_req_pwd", req_pwd);
        session.setAttribute("admin_login_req_code", req_code);
//        System.out.println("登录页面的code：" + session.getAttribute("code"));


        // 在数据库中取值
        try {

            String sql = "select admin_name as userName, pwd from sys_admin";

            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
            List<SysUser> adminList = queryRunner.query(sql, new BeanListHandler<>(SysUser.class));
            System.out.println(adminList);
            for (SysUser admin : adminList) {
                if (req_admin.equals(admin.getUserName()) && req_pwd.equals(admin.getPwd())) {
                    flag = true;
                    session.setAttribute("Admin", admin);
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        // flag为true时进行数据保存处理；
        if (flag) {
            request.setAttribute("admin_login_info", "");
            // 判断验证码是否正确
            if (req_code != null && req_code.equals(session.getAttribute("code"))) {
                request.setAttribute("admin_login_code_info", "");

                // 判断用户是否记住用户名和密码
                if (req_remember != null && req_remember.equals("1")) {
                    // 设置cookie失效时间
                    cookie.setMaxAge(60 * 100000);
                    // 给响应添加cookie
                    response.addCookie(cookie);
                } else {
                    // 给响应添加cookie
                    response.addCookie(cookie);
                }

                // 跳转到/index.jsp页面
                request.getRequestDispatcher("/admin/index.jsp").forward(request, response);

            } else {
//                System.out.println("验证码不正确");
                // 将错误信息储存到域对象中
                request.setAttribute("admin_login_code_info", "验证码不正确");
                // 跳转到login.jsp页面，用户重新输入
                request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
            }
        } else {
            // 将错误信息储存到域对象中
            request.setAttribute("admin_login_info", "用户不存在或密码错误");
            // 跳转到login.jsp页面，用户重新输入
            request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
        }
    }

    // 新闻列表
 /*   public List<News> newsDataf() {
        List<News> newsDataList = AdminService.getNewsData();
        return newsDataList;
    }

    // 用户列表
public List<SysUser> userDataf(){
        List<SysUser> userDataList = AdminService.getUserData();
        return userDataList;
}*/
}
